package com.cloudbroker.bcs.common.emoji;

import java.io.UnsupportedEncodingException;
import java.util.Collections;
import java.util.List;

/**
 * Created by xuyn15828 on 2016/4/26.
 */
public class Emoji {
    private final String description;
    private final boolean supportsFitzpatrick;
    private final List<String> aliases;
    private final List<String> tags;
    private final String unicode;
    private final String htmlDec;
    private final String htmlHex;

    protected Emoji(String description, boolean supportsFitzpatrick, List<String> aliases, List<String> tags, byte... bytes) {
        this.description = description;
        this.supportsFitzpatrick = supportsFitzpatrick;
        this.aliases = Collections.unmodifiableList(aliases);
        this.tags = Collections.unmodifiableList(tags);
        int count = 0;

        try {
            this.unicode = new String(bytes, "UTF-8");
            int e = this.getUnicode().length();
            String[] pointCodes = new String[e];
            String[] pointCodesHex = new String[e];

            int codePoint;
            for(int offset = 0; offset < e; offset += Character.charCount(codePoint)) {
                codePoint = this.getUnicode().codePointAt(offset);
                pointCodes[count] = String.format("&#%d;", new Object[]{Integer.valueOf(codePoint)});
                pointCodesHex[count++] = String.format("&#x%x;", new Object[]{Integer.valueOf(codePoint)});
            }

            this.htmlDec = this.stringJoin(pointCodes, count);
            this.htmlHex = this.stringJoin(pointCodesHex, count);
        } catch (UnsupportedEncodingException var12) {
            throw new RuntimeException(var12);
        }
    }

    private String stringJoin(String[] array, int count) {
        String joined = "";

        for(int i = 0; i < count; ++i) {
            joined = joined + array[i];
        }

        return joined;
    }

    public String getDescription() {
        return this.description;
    }

    public boolean supportsFitzpatrick() {
        return this.supportsFitzpatrick;
    }

    public List<String> getAliases() {
        return this.aliases;
    }

    public List<String> getTags() {
        return this.tags;
    }

    public String getUnicode() {
        return this.unicode;
    }

    public String getUnicode(Fitzpatrick fitzpatrick) {
        if(!this.supportsFitzpatrick()) {
            throw new UnsupportedOperationException("Cannot get the unicode with a fitzpatrick modifier, the emoji doesn\'t support fitzpatrick.");
        } else {
            return fitzpatrick == null?this.getUnicode():this.getUnicode() + fitzpatrick.unicode;
        }
    }

    public String getHtmlDecimal() {
        return this.htmlDec;
    }

    public String getHtmlHexidecimal() {
        return this.htmlHex;
    }

    public boolean equals(Object other) {
        return other != null && other instanceof Emoji && ((Emoji)other).getUnicode().equals(this.getUnicode());
    }

    public int hashCode() {
        return this.unicode.hashCode();
    }

    public String toString() {
        return "Emoji{description=\'" + this.description + '\'' + ", supportsFitzpatrick=" + this.supportsFitzpatrick + ", aliases=" + this.aliases + ", tags=" + this.tags + ", unicode=\'" + this.unicode + '\'' + ", htmlDec=\'" + this.htmlDec + '\'' + ", htmlHex=\'" + this.htmlHex + '\'' + '}';
    }
}
